package com.lightjet.macross.base.upms.controller;

import com.lightjet.macross.base.feign.api.annotation.SecurityCheck;
import com.lightjet.macross.base.feign.api.controller.PrincipalController;
import com.lightjet.macross.base.upms.domain.ApiRules;
import com.lightjet.macross.base.upms.service.ApiRulesService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/apirules")
@Scope("request")
@Log
@RefreshScope
public class ApiRulsController extends PrincipalController {

    @Resource
    private ApiRulesService apiRulesService;

    // 根据查询条件获取api规则集合
    @GetMapping("/list")
    @SecurityCheck("根据查询条件获取api规则集合")
    public ResponseDTO<List<ApiRules>> GetListByFilters(String servicename,String apiuri,String clients) {
        return RespUtil.CreateOK(apiRulesService.FindByFilters("%"+servicename+"%","%"+apiuri+"%","%"+clients+"%"));
    }

    // 根据（服务名/apiuri）查询规则
    @GetMapping("/uri")
    public ResponseDTO<ApiRules> GetRuleByPath(String servicename,String acturi) {
        return RespUtil.CreateOK(apiRulesService.FindByUri(servicename,acturi));
    }

    @PostMapping("")
    public ResponseDTO<ApiRules> CreateApiRule(@RequestBody ApiRules apiRules) {
        ApiRules has = apiRulesService.FindByServicenameAndApiuri(apiRules.getServicename(),apiRules.getApiuri());
        if(has == null)
        {
            return RespUtil.CreateOK(apiRulesService.CreateAndFlush(apiRules));
        }
        else
        {
            return RespUtil.CreateError("新增出错：规则已存在",has);
        }
    }

    @PutMapping("")
    public ResponseDTO<ApiRules> UpdateApiRule(@RequestBody ApiRules apiRules) {
        ApiRules has = apiRulesService.FindDistinctById(apiRules.getId());
        if(has != null)
        {
            return RespUtil.CreateOK(apiRulesService.MergeAndFlush(apiRules));
        }
        else
        {
            return RespUtil.CreateError("更新出错：规则【"+apiRules.getId()+"】不存在",has);
        }
    }

    @DeleteMapping("/batch")
    @Transactional
    public ResponseDTO<String> DeleteApiRuleBatch(@RequestBody List<Integer> ids) {
        try
        {
            apiRulesService.DeleteInBatch(ids.stream().map(ApiRules::new).collect(Collectors.toList()));
            return RespUtil.CreateOK("批量删除成功！");
        }
        catch (Exception e)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespUtil.CreateError("批量删除失败[已回滚]："+ e.getMessage());
        }

    }

    @DeleteMapping("/cache")
    public ResponseDTO<String> ClearChace() {
        return apiRulesService.ClearRuleCache();

    }

}
